﻿using CommonDevelop;
using CommonEngine;
using CommonFP;
using ManageDto;
using ManageService;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using WebAdmin.Handler;

namespace WebAdmin.Areas.Org.Controllers
{
    /// <summary>
    /// 菜单模块
    /// </summary>
    [Area("Org")]
    [Route("Org/[controller]/[action]")]
    public class ModuleController : BaseController
    {
        private readonly IModuleService _ModuleService;
        /// <summary>
        /// 构造注入
        /// </summary>
        public ModuleController(IModuleService ModuleService)
        {
            this._ModuleService = ModuleService;
        }

        /// <summary>
        /// 列表页
        /// </summary>
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Title = "系统模块";
            return View();
        }

        /// <summary>
        /// 选择父级模块(显示列表树，不同于组件树)
        /// </summary>
        [HttpGet]
        public ActionResult GetSelectLayer()
        {
            ViewBag.Title = "父级模块";
            return View();
        }

        #region--Select--

        /// <summary>
        /// 模块树
        /// </summary>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeList(string keyword)
        {
            List<ModuleDto> data = this._ModuleService.GetList<ModuleDto>(keyword).ToList();
            return this.SuccessJson(data);
        }

        #endregion

        #region--增删改--

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeAdd()
        {
            ModuleDto en = new ModuleDto();
            en.ModuleCode = this._ModuleService.BuildSeq();
            en.IsMenu = true;
            en.IsPublic = true;
            return this.SuccessJson(en);
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeDelete(string keyValue)
        {
            Validation<bool> vb = this._ModuleService.Delete(keyValue);
            var ValTuple = vb.Outcome(a => a);
            if (ValTuple.Errors.IsEmpty())
                return ValTuple.result ? this.Success("操作成功") : this.Fail("操作失败");
            else
                return this.Fail(ValTuple.Errors);
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult NodeEdit(string keyValue)
        {
            var outcome = this._ModuleService.Get<ModuleDto>(keyValue);
            return outcome.Match(() => this.Fail("Null Error"),
                en =>
                {
                    en.ParentName = this._ModuleService.GetParentName(en.ParentId);
                    return this.SuccessJson(en);
                });
        }

        [HttpPost]
        [AjaxOnly]
        [DisableRequestSizeLimit]
        public ActionResult NodeSave([FromForm]string DataText)
        {
            ModuleDto em = PageUtility.FormMap<ModuleDto>(DataText);
            if (em == null)
                return this.Fail("表单解读失败");
            if (em.FullName.IsEmpty())
                return this.Fail("模块名称不能为空");
            if (em.IsMenu)
                if (em.ActionAddress.IsEmpty())
                    return this.Fail("模块是菜单时，MVC地址不能为空");

            if (em.ActionAddress.IsEmpty())
                em.ActionAddress = "#";

            if (em.IndexAddress.IsEmpty())
                em.IndexAddress = em.ActionAddress;

            em.TargetEnReg = "";
            em.ResourceTypeName = "";

            Validation<bool> vb = FP.Error("未执行");
            if (em.ModifyUserId.IsEmpty())
                vb = this._ModuleService.Add(em);
            else
                vb = this._ModuleService.Save(em);

            var ValTuple = vb.Outcome(a => a);
            if (ValTuple.Errors.IsEmpty())
                return ValTuple.result ? this.Success("操作成功") : this.Fail("操作失败");
            else
                return this.Fail(ValTuple.Errors);
        }

        #endregion
    }
}
